package com.zhou.controller;


import com.zhou.service.AdminService;


import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhou.utis.dto.LoginDto;
import com.zhou.utis.result.Result;

import javax.annotation.Resource;


/**
 * @author zhouyuyan
 */

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    /**
     * 发送验证码
     *
     * @param phone
     * @return
     */
    @PostMapping("code_msg")
    public Result code(String phone) {
        try {
            Integer code = adminService.code(phone);
            if (code == 0) {
                return Result.error();
            }
            return Result.success(code);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 登录接口
     *
     * @return
     */
    @PostMapping("/login")
    public Result login(String phone, String code) {
        try {
            LoginDto login = adminService.login(phone, code);
            if (StringUtils.isEmpty(login)) {
                return Result.error();
            }
            return Result.success(login);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 登出
     */
    @PostMapping("/out")
    public Result out(String phone) {
        try {
            Boolean out = adminService.out(phone);
            if (!out) {
                return Result.error("登出失败1");
            }
            return Result.success("登出成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
